package funtastic.spellingbee;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class CustomSpellingListActivity extends Activity
{
    private static final int DIALOG_CREATE_CUSTOM_SPELLING_LIST = 0;
    private static final int DIALOG_UPDATE_CUSTOM_SPELLING_LIST = 1;

    private ListView         mCustomSpellingList;
    private String           mCustomSpellingListNameCurrent;
    private EditText         mCustomSpellingListNameNew;
    private EditText         mCustomSpellingListNameUpdate;
    private SpellingBee      mSpellingBee;

    
    @Override
    public boolean onContextItemSelected(MenuItem item)
    {
        if (item.getItemId() == R.id.menuItemDisplayCustomSpellings)
        {
            startActivity(new Intent(this, CustomSpellingsActivity.class).putExtra("CustomSpellingList", mCustomSpellingListNameCurrent));
        }
        else if (item.getItemId() == R.id.menuItemEditCustomSpellingList)
        {
            showDialog(DIALOG_UPDATE_CUSTOM_SPELLING_LIST);
        }
        else if (item.getItemId() == R.id.menuItemDeleteCustomSpellingList)
        {
            AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle("Confirm Custom Spelling List Deletion").setMessage("Are you sure you want to delete " + mCustomSpellingListNameCurrent + "?").setCancelable(false)
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface dialog, int id)
                        {
                            mSpellingBee.deleteCustomSpellingList(mCustomSpellingListNameCurrent);

                            refreshCustomSpellingList();
                        }
                    }).setNegativeButton("No", new DialogInterface.OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface dialog, int id)
                        {
                            dialog.cancel();
                        }
                    }).create();
            alertDialog.show();
        }
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.custom_activity);
        setTitle(getString(R.string.app_name) + " - Custom Spelling Lists");

        mCustomSpellingList = (ListView) findViewById(R.id.listViewItems);
        mSpellingBee = (SpellingBee) getApplication();

        mCustomSpellingList.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item_practice, mSpellingBee.getSpellingLists(getString(R.string.list_type_custom))));

        registerForContextMenu(mCustomSpellingList);
    }

    @Override
    public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenuInfo menuInfo)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_custom_spelling_list, contextMenu);

        AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        mCustomSpellingListNameCurrent = ((TextView) info.targetView).getText().toString();
        contextMenu.setHeaderTitle(mCustomSpellingListNameCurrent);
    }

    @Override
    protected Dialog onCreateDialog(int id)
    {
        LayoutInflater factory = LayoutInflater.from(this);
        switch (id)
        {
            case DIALOG_CREATE_CUSTOM_SPELLING_LIST:
                final View newCustomSpellingList = factory.inflate(R.layout.new_custom_spelling_list, null);
                mCustomSpellingListNameNew = (EditText) newCustomSpellingList.findViewById(R.id.editTextCustomList);
                Dialog newCustomSpellingListDialog = new AlertDialog.Builder(this).setIcon(R.drawable.ic_menu_agenda).setTitle(R.string.add_custom_list).setView(newCustomSpellingList)
                        .setPositiveButton(R.string.save, new DialogInterface.OnClickListener()
                        {
                            @Override
                            public void onClick(DialogInterface dialog, int id)
                            {
                                String errorMessage = mSpellingBee.addCustomSpellingList(mCustomSpellingListNameNew.getText().toString());

                                if (errorMessage.length() == 0)
                                {
                                    refreshCustomSpellingList();
                                }
                                else
                                {
                                    showMessage(errorMessage);
                                }
                            }
                        }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener()
                        {
                            @Override
                            public void onClick(DialogInterface dialog, int id)
                            {
                                dialog.cancel();
                            }
                        }).create();
                newCustomSpellingListDialog.setOnShowListener(new DialogInterface.OnShowListener()
                {
                    @Override
                    public void onShow(DialogInterface arg0)
                    {
                        mCustomSpellingListNameNew.setText("");
                    }
                });
                return newCustomSpellingListDialog;

            case DIALOG_UPDATE_CUSTOM_SPELLING_LIST:
                final View updateCustomSpellingList = factory.inflate(R.layout.new_custom_spelling_list, null);
                mCustomSpellingListNameUpdate = (EditText) updateCustomSpellingList.findViewById(R.id.editTextCustomList);
                Dialog updateCustomSpellingListDialog = new AlertDialog.Builder(this).setIcon(R.drawable.ic_menu_agenda).setTitle(R.string.edit_custom_list).setView(updateCustomSpellingList)
                        .setPositiveButton(R.string.save, new DialogInterface.OnClickListener()
                        {
                            @Override
                            public void onClick(DialogInterface dialog, int id)
                            {
                                dialog.cancel();

                                if (!mCustomSpellingListNameCurrent.equals(mCustomSpellingListNameUpdate.getText().toString()))
                                {
                                    String errorMessage = mSpellingBee.updateCustomSpellingList(mCustomSpellingListNameCurrent, mCustomSpellingListNameUpdate.getText().toString());

                                    if (errorMessage.length() == 0)
                                    {
                                        refreshCustomSpellingList();
                                    }
                                    else
                                    {
                                        showMessage(errorMessage);
                                    }
                                }
                            }
                        }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener()
                        {
                            @Override
                            public void onClick(DialogInterface dialog, int id)
                            {
                                dialog.cancel();
                            }
                        }).create();
                updateCustomSpellingListDialog.setOnShowListener(new DialogInterface.OnShowListener()
                {
                    @Override
                    public void onShow(DialogInterface arg0)
                    {
                        mCustomSpellingListNameUpdate.setText(mCustomSpellingListNameCurrent);
                    }
                });
                return updateCustomSpellingListDialog;
            default:
                break;
        }
        return super.onCreateDialog(id);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_new_custom_spelling_list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.itemNew:
                showDialog(DIALOG_CREATE_CUSTOM_SPELLING_LIST);
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void refreshCustomSpellingList()
    {
        mCustomSpellingList.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item_practice, mSpellingBee.getSpellingLists(getString(R.string.list_type_custom))));
    }

    public void showMessage(String message)
    {
        AlertDialog alertDialog = new AlertDialog.Builder(this).setMessage(message).setCancelable(false).setPositiveButton("Ok", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int id)
            {
                dialog.cancel();
            }
        }).create();
        alertDialog.show();
    }
}
